package com.englishstudymachine;

import java.io.BufferedReader;
import java.io.File;
import java.util.regex.Pattern;  
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;

public class MyCharacterGenerator implements Runnable {
public ArrayList<MyCharacter> Characters;
private BufferedReader buff_f;

	public MyCharacterGenerator(File fileObj)
	{
		Characters=new ArrayList<MyCharacter>();
		FileInputStream f=null;
		try {
		
			 f= new FileInputStream(fileObj) ;
		
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (f!=null)
		{
			
			 try {
				buff_f=new BufferedReader(new InputStreamReader(f,new FileReader(fileObj).getEncoding()));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			 Thread h=new Thread(this);
			 h.start();

		}
		
	}
	public int getNumByTime(long time)
	{
		return getNumByTime(time,0);
	}
	public int getNumByTime(long time,int lastNum)
	{
		int i=lastNum;
		int max=getReadedCharacter();
		MyCharacter c;
		for(;i<max;i++)
		{
			c =getCharacter(i);
			if(c!=null)
			{
			  if(c.beginTime<=time && c.endTime>=time)
			  {
				  return i;
			  }
			}
		}
		return -1;
	}
	public int getReadedCharacter()
	{
		return Characters.size();
	}
	public MyCharacter getCharacter(int num)
	{
		int max=getReadedCharacter();
		if(num<0 || max-1<num) return null;
		return Characters.get(num);
	}

	public static void Test_1(MyCharacterGenerator aa,String saveFilePath)
	{
		/*
		StringBuilder builer=new StringBuilder();
		for(int i=0;i<aa.Characters.size();i++)
		{
			MyCharacter k=aa.Characters.get(i);
			builer.append(k.number+Tools.newLine);
			builer.append(Srt_analyzing.toTimeString(k.beginTime, k.endTime)+Tools.newLine);
			if(!k.character_ch.equals(""))
			{
			builer.append(k.character_ch+Tools.newLine);
			}
			if(!k.character_en.equals(""))
			{
			builer.append(k.character_en+Tools.newLine);
			}
			builer.append(Tools.newLine);
		}
		Tools.SaveFile(saveFilePath, builer.toString());
		*/
		
	}

	@Override
	public void run() {
		
			Characters=Srt_analyzing.getCharacters(buff_f);
		
		
	}

}
